<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./css/bootstrap-v4.6.0.css" />
    <link rel="stylesheet" href="./css/login.css" />
  </head>

  <body>
    <div class="login-box">
      <div class="form-group">
        <label for="username">Account</label>
        <!-- 账号 -->
        <input type="text" class="form-control" id="username" autocomplete="off" />
        <small id="emailHelp" class="form-text text-muted">The available account is <strong>admin</strong></small>
      </div>
      <div class="form-group">
        <!-- 密码 -->
        <label for="password">Password</label>
        <input type="password" class="form-control" id="password" />
        <small id="emailHelp" class="form-text text-muted">The available password is <strong>123456</strong></small>
      </div>
      <button type="submit" class="btn btn-primary" id="btnLogin">Submit</button>
    </div>

    <script src="./lib/axios.js"></script>
    <script>
      // 1. 获取按钮表单
      const btn = document.querySelector('#btnLogin')
      const user_ipt = document.querySelector('#username')
      const pwd_ipt = document.querySelector('#password')

      // 接口地址
      const api = 'http://ajax-api.itheima.net/api/login'

      // 2. 注册事件
      btn.addEventListener('click', () => {
        // 箭头函数没有this
        // 点击按钮的时候，获取usrename和password的值 
        const username = user_ipt.value
        const password = pwd_ipt.value

        // 3. 发送请求
        axios.post(api, {
          // ES6 的简写
          username,
          password
        }).then(res => {
          // console.log(res)
          alert(res.data.message)
        }).catch(err => {
          // console.log(err)
          console.dir(err)
          alert(err.response.data.message)
          // alert('登录失败')
        })
        
      })
    </script>
  </body>
</html>
